import os
import wx.gizmos

from . import retreiver_xrc

from app.util import pluginBase, files

class RetreiverPanel(retreiver_xrc.xrcRetPanel, pluginBase.PluginMixin):
    """Log retreiver panel."""

    name = "Log file retreiver"

    _defaultPrograms = ("dmesg", "logread")
    _defaultFiles = ("/tmp/x.log", )

    def __init__(self, parent, app):
        super(RetreiverPanel, self).__init__(parent)
        self._mixinInit(parent, app)

        self.configDefaultVal("savedPrograms", self._defaultPrograms)
        self.configDefaultVal("savedFiles", self._defaultFiles)
        self.configDefaultVal("targetDir", files.getDefaultSavedFileDir())
        self.configDefaultVal("clearDir", False)

        self._configGuiMap = (
            (self.clearDir, "clearDir"),
            (self.saveDirCtrl, "targetDir"),
            (self.progOutLBox, "savedPrograms"),
            (self.fileSaveLBox, "savedFiles"),
        )
        self.setControls()

    def _dumpData(self):
        """Dump data from given files to files on disc."""
        for _fname in self._config["savedFiles"]:
            _outFname = os.path.join(
                self._config["targetDir"], files.normalizeFilename(_fname))
            _f = file(_outFname, "wb")
            self._app.saveFromRemote(("cat", _fname), _f)
            _f.close()
        for _prog in self._config["savedPrograms"]:
            _outFname = os.path.join(self._config["targetDir"],
                files.normalizeFilename("%s_out.txt" % _prog))
            _f = file(_outFname, "wb")
            self._app.saveFromRemote(_prog, _f)
            _f.close()

    def OnButton_doSave(self, evt):
        """On 'Save' button press."""
        self.getControls()
        if self._config["clearDir"]:
            files.emptyDir(self._config["targetDir"])
        self._dumpData()

# vim: set sts=4 sw=4 et :
